import java.util.*;

public class BinaryTree {

    static class TreeNode {
        public char val;
        //存储左孩子结点的引用
        public TreeNode left;
        //存储右孩子结点的引用
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        E.right = H;
        C.left = F;
        C.right = G;

        return A;
    }

    /**
     * 层序遍历
     * @param root
     */
    public void levelOrder(TreeNode root){
        if(root == null)
            return;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            int size = queue.size();
            while(size != 0){
                TreeNode cur = queue.poll();
                System.out.print(cur.val + " ");
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
                size--;
            }


        }
        System.out.println();
    }

    /**
     *二叉树的层序遍历
     * @param root
     * @return
     */
    public List<List<Character>>levelOrder1(TreeNode root){
        List<List<Character>> ret = new ArrayList<>();
        if(root == null) {
            return ret;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            //一层的数据
            List<Character> curList = new ArrayList<>();
            int size = queue.size();
            while(size != 0){
                TreeNode cur = queue.poll();
                // System.out.print(cur.val + " ");
                curList.add(cur.val);
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
                size--;
            }
            ret.add(curList);
        }
        return ret;
    }

    /**
     *翻转二叉树
     * @param root
     * @return
     */
    public TreeNode invertTree(TreeNode root) {
        if(root == null) {
            return null;
        }
        if(root.left == null && root.right == null){
            return root;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;

        invertTree(root.left);
        invertTree(root.right);
        return root;
    }


    /**
     * 判断平衡二叉树
     * 时间复杂度 O(n^2)
     * @param root
     * @return
     */
    public boolean isBalanced(TreeNode root) {
        if(root == null) {
            return true;
        }
        int leftHeight = maxDepth(root.left);
        int rightHeight = maxDepth(root.right);

        return Math.abs(leftHeight-rightHeight) <= 1
                && isBalanced(root.left)
                && isBalanced(root.right);
    }
    public int maxDepth(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftH = maxDepth(root.left);
        int rightH = maxDepth(root.right);
        return Math.max(leftH,rightH)+1;

    }

    /**
     * 判断平衡二叉树
     * 时间复杂度 O(n)
     * @param root
     * @return
     */
    public boolean isBalanced1(TreeNode root) {
        if(root == null) {
            return true;
        }
        return maxDepth(root) >= 0;
    }
    public int maxDepth1(TreeNode root){
        if(root == null) {
            return 0;
        }
        int leftH = maxDepth1(root.left);
        int rightH = maxDepth1(root.right);
        if(leftH >= 0 && rightH >= 0 && Math.abs(leftH - rightH) <= 1) {
            return  Math.max(leftH,rightH)+1;
        }else {
            //证明 高度差是>=2   不平衡的
            return -1;
        }
    }


    /**
     * 判断当前树，是不是镜像对称的
     * @param root
     * @return
     */
    public boolean isSymmetric(TreeNode root) {
        if(root == null){
            return true;
        }
        return isSymmetricChild(root.left,root.right);
    }

    public boolean isSymmetricChild(TreeNode leftTree,TreeNode rightTree) {
        if(leftTree == null && rightTree != null
            || leftTree != null && rightTree == null) {
            return false;
        }
        if(leftTree == null && rightTree == null) {
            return true;
        }
        if(leftTree.val != rightTree.val) {
            return false;
        }
        return isSymmetricChild(leftTree.left,rightTree.right)
                && isSymmetricChild(leftTree.right,rightTree.left);
    }

    /**
     * 给你一颗二叉树，判断是不是完全二叉树
     * @param root
     * @return
     */
    public boolean isCompleteTree(TreeNode root) {
        if(root == null){
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }
        //再次判断对列，是不是里面都是null
        while (!queue.isEmpty()) {
            TreeNode cur = queue.peek();
            if(cur == null) {
                queue.poll();
            }else{
                return false;
            }
        }
        return true;
    }


    /**
     *二叉树的最近公共祖先
     * @param root
     * @param p
     * @param q
     * @return
     */
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null){
            return null;
        }
        if(root == p || root == q){
            return root;
        }
        TreeNode leftT = lowestCommonAncestor(root.left,p,q);
        TreeNode rightT = lowestCommonAncestor(root.right,p,q);
        if(leftT != null && rightT != null) {
            return root;
        } else if(leftT != null) {
            return leftT;
        } else if(rightT != null) {
            return rightT;
        }
        return null;
    }


    /**
     * 找到root到指定节点node路径上的所有节点，存储到stack中
     * @param root
     * @param node
     * @param stack
     * @return
     */
    public boolean getPath(TreeNode root, TreeNode node, Stack<TreeNode> stack) {
        if(root == null) {
            return false;
        }
        stack.pop();
        if(root == node) {
            return true;
        }
        stack.push(root);
        boolean flg = getPath(root.left,node,stack);
        if(flg) {
            return true;
        }
        flg = getPath(root.right,node,stack);
        if(flg) {
            return true;
        }
        return false;
    }
    public TreeNode lowestCommonAncestor2(TreeNode root,TreeNode p,TreeNode q) {
        if(root == null){
            return null;
        }
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack<>();
        getPath(root,p,stack1);
        getPath(root,p,stack2);
        //上诉代码已经能够求到root到指定节点路径上的所有节点
        int size1 = stack1.size();
        int size2 = stack2.size();
        int size = size1 - size2;
        if(size < 0) {
            size = Math.abs(size);
            while(size != 0) {
                stack2.pop();
                size--;
            }
        }else {
            while(size != 0) {
                stack1.pop();
                size--;
            }
        }
        //两个栈的大小是一样的
        while(!stack1.isEmpty() && !stack2.isEmpty()) {
            if(stack1.peek() == stack2.peek()) {
                return stack1.pop();
            }else{
                stack1.pop();
                stack2.pop();
            }
        }
        return null;
    }


    /**
     * 根据二叉树创建字符串
     * @param root
     * @return
     */
    public String tree2str(TreeNode root){
        StringBuilder stringBuilder = new StringBuilder();
        tree2strChild(root,stringBuilder);
        return stringBuilder.toString();
    }
    public void tree2strChild(TreeNode root,StringBuilder stringBuilder) {
        if(root == null){
            return;
        }
        stringBuilder.append(root.val);
        //判断根的左子树
        if(root.left != null){
            stringBuilder.append("(");
            tree2strChild(root.left,stringBuilder);
            stringBuilder.append(")");
        } else {
            if(root.right == null) {
                return;
            }else{
                stringBuilder.append("()");
            }
        }
        //判断根的右子树
        if(root.right != null){
            stringBuilder.append("(");
            tree2strChild(root.right,stringBuilder);
            stringBuilder.append(")");
        }else{
            return;
        }
    }


    /**
     * 前序遍历，非递归
     * @param root
     */
    public void preOrderNor(TreeNode root){
        if(root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode top = null;
        while (cur != null || !stack.isEmpty()) {
            while(cur != null) {
                stack.push(cur);
                System.out.println(cur.val + " ");
                cur = cur.left;
            }
            top = stack.pop();
            cur = top.right;
        }
        System.out.println();
    }


    /**
     * 中序遍历 非递归
     * @param root
     */
    public void inOrderNor(TreeNode root){
        if(root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode top = null;
        while (cur != null || !stack.isEmpty()) {
            while(cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            top = stack.pop();
            System.out.println(cur.val + " ");
            cur = top.right;
        }
        System.out.println();
    }


    /**
     * 后续遍历，非递归
     * @param root
     */
    public void postOrderNor(TreeNode root){
        if(root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode top = null;
        TreeNode prev = null;
        while(cur != null || !stack.isEmpty()) {
            while(cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            top = stack.peek();
            if(top.right == null || top.right == prev) {
                stack.pop();
                System.out.print(top.val+" ");
                prev = top;
            }else{
                cur = top.right;
            }
        }
        System.out.println();
    }
}